package com.neuedu.funlifebbs.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import com.neuedu.funlifebbs.bean.Users;
import com.neuedu.funlifebbs.query.UsersQuery;
import com.neuedu.funlifebbs.service.UsersService;
import com.neuedu.funlifebbs.utils.JsonModel;
import com.neuedu.funlifebbs.utils.StringUtil;

/**
 * 
 * @author lzq
 *
 */
@Controller
@RequestMapping("admin/users")
public class UserController {

	@Autowired
	UsersService usersService;

	@RequestMapping(value = "/listUI", method = RequestMethod.GET)
	public String center() {
		return "back/users/list";
	}

	/**
	 * 分页查询显示在用户列表页面
	 * 
	 * @param usersQuery
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel list(UsersQuery usersQuery) throws IOException {
		PageInfo<Users> pageInfo = usersService.query(usersQuery);
		JsonModel jsonModel = new JsonModel();
		jsonModel.setMsg("查找成功");
		jsonModel.setSuccess(true);
		jsonModel.setTotal(pageInfo.getTotal());
		jsonModel.setRows(pageInfo.getList());
		return jsonModel;
	}

	/**
	 * 跳转编辑用户信息
	 * 
	 * @param model
	 * @param id
	 * @return
	 */

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editUi(Model model, @PathVariable int id) {
		//
		Users users = usersService.findById(id);
		model.addAttribute("users", users);
		return "back/users/edit";
	}

	/**
	 * 修改用户信息
	 * 
	 * @param model
	 * @param users
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String alterUsers(Model model, Users users,
			HttpServletRequest request) {
		String errorMsg = null;
		if (users.getId() != 0) {
			usersService.update(users);
		} else {
			errorMsg = "权限或状态的修改只能为0或1";
		}
		model.addAttribute("errorMsg", errorMsg);
		return "redirect:/admin/users/listUI";
	}

	/**
	 * 跳转登陆注册页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "register", method = RequestMethod.GET)
	public String toRegister() {
		return "login";
	}

	/**
	 * 注册用户
	 * 
	 * @param model
	 * @param users
	 * @return
	 */
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public String register(Model model, Users users) {

		String errorMsg;
		if (StringUtil.isEmpty(users.getName())
				|| StringUtil.isEmpty(users.getPassword())
				|| StringUtil.isEmpty(users.getEmail())) {
			errorMsg = "账号密码邮箱不能为空";
		} else {
			usersService.register(users);
			errorMsg = "注册成功";
		}
		model.addAttribute("errorMsg", errorMsg);

		return "login";
	}

}
